/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.ssau.entities;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import javax.persistence.*;
import org.jdom.Element;
import ru.ssau.lib.PresentableAsStringIntegerPair;
import ru.ssau.lib.StringIntegerPair;
import ru.ssau.lib.XMLizable;
import ru.ssau.utils.JMSUtil;
import ru.ssau.utils.XMLHelper;


@Entity
@Table(name="Threads")
@NamedQueries({
    @NamedQuery(name=Thread.FIND_ALL_THREADS, query="SELECT t FROM Thread t"),
    @NamedQuery(name=Thread.FIND_THREADS_BY_CAPTION, query="SELECT t FROM Thread t WHERE UPPER(t.caption) LIKE ?1")})
public class Thread implements Serializable, PresentableAsStringIntegerPair, XMLizable {

    @Transient
    public static final String FIND_ALL_THREADS = "findAllThreads";
    @Transient
    public static final String FIND_THREADS_BY_CAPTION = "findThreadsByCaption";

    @Id
    @Column(name="ThreadID", nullable=false)
    @SequenceGenerator(name="ThreadIdGenerator", sequenceName="ThreadIDSeq", allocationSize=1)
    @GeneratedValue(generator="ThreadIdGenerator")
    private Integer id;

    @Column(nullable=false)
    private String caption;

    @OneToMany(mappedBy="thread", fetch=FetchType.LAZY, cascade={CascadeType.REMOVE})
    private Collection<Message> messages;

    public Thread() {

    }

    public Thread(String caption) {
        this.caption = caption;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCaption() {
        return caption;
    }

    public void setCaption(String caption) {
        this.caption = caption;
    }

    public Collection<Message> getMessages() {
        return messages;
    }

    public void setMessages(Collection<Message> messages) {
        this.messages = messages;
    }

    @PostPersist
    private void sendThreadCreatedLogMessage() {
        System.out.println("created");
        JMSUtil.sendLogMessage(JMSUtil.THREAD, id, new Date(), JMSUtil.ADD);
    }

    @PostRemove
    private void sendThreadRemovedLogMessage() {
        System.out.println("removed");
        JMSUtil.sendLogMessage(JMSUtil.THREAD, id, new Date(), JMSUtil.REMOVE);
    }

    @PostUpdate
    private void sendThreadUpdatedLogMessage() {
        System.out.println("updated");
        JMSUtil.sendLogMessage(JMSUtil.THREAD, id, new Date(), JMSUtil.UPDATE);
    }

    @Override
    public boolean equals(Object obj) {
        if(obj == null) {
            return false;
        }
        if(obj == this) {
            return true;
        }
        if(!(obj instanceof ru.ssau.entities.Thread)) {
            return false;
        }
        ru.ssau.entities.Thread other = (ru.ssau.entities.Thread) obj;
        
        return id.equals(other.id) && caption.equals(other.caption);
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 11 * hash + (this.id != null ? this.id.hashCode() : 0);
        hash = 11 * hash + (this.caption != null ? this.caption.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getName());
        sb.append(": [id=");
        sb.append(id);
        sb.append(", caption=");
        sb.append(caption);
        sb.append("]");
        return sb.toString();
    }

    public Element toXMLElement() {
        Element root = new Element("Thread");
        XMLHelper.addElement(root, "id", id);
        XMLHelper.addElement(root, "caption", caption);
        return root;
    }

    public void loadFromXMLElement(Element elmnt) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public StringIntegerPair toStringIntegerPair() {
        return new StringIntegerPair(id, caption);
    }

}
